package courseagenda;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStreamWriter;
import java.util.*;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.HandlerBase;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.ibm.infosphere.sirius.agent.config.WatcherConfigDescriptor;
import com.ibm.infosphere.sirius.agent.resources.Global;

public class Course extends DefaultHandler {
	
	private String courseName;
	private List<String> assignments;
	private List<String> announcements;
	private Map<String, Integer> grades;
	private Properties courses;
	
	private SAXParserFactory saxParserFactory = null;
	private SAXParser saxParser = null;
	private String readValue;
	
	private String currentAssignment;
	
	public Course(String courseFile)
	{
		assignments=new ArrayList<String>();
        announcements = new ArrayList<String>();
        courses = new Properties();
	}
	
	public void addAssignment(String newAssignment)
    {
        assignments.add(newAssignment);
    }

    public List<String> getAssignments()
    {
        return assignments;
    }

    public void addAnnouncement(String newAnnouncement)
    {
        announcements.add(newAnnouncement);
    }

    public List<String> getAnnounements()
    {
        return announcements;
    }

    public void addGrade(String assignment, int grade)
    {
        grades.put(assignment, new Integer(grade));
    }

    public Map<String, Integer> getGrades()
    {
        return grades;
    }
    
    public boolean readCourses(String courseFile)
    {
    	try {
    		saxParserFactory = SAXParserFactory.newInstance();
			saxParser = saxParserFactory.newSAXParser();
			FileInputStream inputStream = new FileInputStream(courseFile);
			saxParser.parse(inputStream, this);
			inputStream.close();
			return true;
    	}
    	catch (Exception e){
    		return false;
    	}
    }
    
    /**
	 * startElement -- SAX Parsing.
	 */
	public void startElement(String namespaceURI, String localName, String elementName, Attributes attrs) 
	throws SAXException {
		if (elementName.equalsIgnoreCase("course")) {
			courseName = attrs.getValue("name");
		} else if (elementName.equalsIgnoreCase("grade")) {
			currentAssignment = attrs.getValue("assignment");
		}
	}
	
	public void endElement(String namespaceURI, String localName, String elementName) 
	throws SAXException {
		// Messages
		//String message = null;
		
		if (elementName.equalsIgnoreCase("assignment")) {
			assignments.add(readValue);
//		} else if (elementName.equalsIgnoreCase(ELEMENT_DIRECTORY)) {
//			filesystemConfigDescriptor.addDirectory(readValue);
		} else if (elementName.equalsIgnoreCase("announcement")) {
			announcements.add(readValue);
		} else if (elementName.equalsIgnoreCase("grade")) {
			grades.put(currentAssignment, new Integer(readValue));
		}
		
	}
	
	public void characters(char[] ch, int start, int length)
	throws SAXException {
		// Messages
		String message = null;
		
		readValue = new String(ch, start, length);
		
		String value = readValue.trim();
	}

}
